Skip to content

3-state boolean columns emphasis on NOT NULL constraint #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

armandmgt
Copy link

@armandmgt armandmgt commented Jan 14, 2025

As discussed in #343, the problem of 3-state boolean columns comes from the nullability of the column.

This PR adds emphasis on this. Default values are only required when adding a column to an existing table so create_table :users { |t| t.boolean :active, null: false } is completely valid.

fixes #343

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Minor notes below.

@pirj pirj requested a review from a team January 14, 2025 15:43
Co-authored-by: Phil Pirozhkov <pirj@users.noreply.github.com>
@armandmgt
Copy link
Author

Hello @koic ! Would you have some time to review this so that we can follow up with the modification of the rubocop cop ?

@pirj
Copy link
Member

pirj commented Jul 30, 2025

@rubocop/style-guide-editors WDYT?

@@ -1167,19 +1167,32 @@ And you'll have to consider the fact that most non-trivial apps share a database

=== 3-state Boolean [[three-state-boolean]]

With SQL databases, if a boolean column is not given a default value, it will have three possible values: `true`, `false` and `NULL`.
With SQL databases, if a boolean column is nullable, it will have three possible values: `true`, `false` and `NULL`.
Boolean operators https://en.wikipedia.org/wiki/Three-valued_logic[work in unexpected ways] with `NULL`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This article is long and math heavy, is there perhaps a better reference?

To avoid such situations, boolean columns should always have a default value and a `NOT NULL` constraint.
To avoid such situations, boolean columns should always have a `NOT NULL` constraint.

Note that when adding a boolean column to an existing table, a default value should be put in place. Otherwise the `NOT NULL` constraint will break for existing rows.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or it can be added without the constraint, backfilled, and have the constraint added afterwards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggestion: 3-state booleans don't require a default
3 participants